/*
 * server.h
 *
 *  Created on: 2012-01-06
 *      Author: mateusz
 */
#define SUBPROCESSES 10
#define SUPPLIER_SLEEP 1
#define CREATOR_SLEEP 1
#define INIT_SUPPLIES 300
#define SUPPLIES_PER_SEC 50
#define SUPPLIES_PER_WORKER 5
#define ATTACK_SLEEP 5
#define WINS_TO_END 5

int newClient(int clientPID, int id);
void removeClient(int pid);
void removeAllClients(int zombies);
void startGame();
void sender(int csQueue);
int findFreeSlot();
void cleanEverything(int serverQueue, int csQueue);
void stopGame();
void stopServer();
void receiver(int csQueue, int serverQueue);
void supplier(int csQueue);
void factory(int serverQueue);
void creator(int csQueue, int serverQueue);
void fillEntities();
void attack(int serverQueue, int csQueue);
void serverBattle();

int sendCreateEntityMsg(int qID, long type);
int getCreateEntityMsg(int qID, long type);

int sendBattleMsg(int qID, long type);
int getBattleMsg(int qID, long type);

struct entityInfo {
	const int id;
	const int cost;
	const double atack;
	const double defense;
	const int createTime;
};

const struct entityInfo lightInfantryInfo = { LIGHT_INFANTRY, 100, 1.0, 1.2, 2 };
const struct entityInfo heavyInfantryInfo = { HEAVY_INFANTRY, 250, 1.5, 3, 3 };
const struct entityInfo cavalryInfo = { CAVALRY, 550, 3.5, 1.2, 5 };
const struct entityInfo workerInfo = { WORKERS, 150, 0, 0, 2 };

struct entityInfo
		entitiesInfo[LAST_ENTITY + 1] = { { SUPPLIES, 0, 0, 0, 0 }, { WORKERS, 150, 0, 0, 2 }, {
				LIGHT_INFANTRY, 100, 1.0, 1.2, 2 }, { HEAVY_INFANTRY, 250, 1.5, 3, 3 }, { CAVALRY, 550, 3.5,
				1.2, 5 } };

enum serverMsgs {
	S_LOG_CMD = 1,
	S_WIN_END_GAME,
	S_ENTITIES_UPDATE,
	S_CREATE_ENTITIES_CMD,
	S_CREATE_ENTITY_CMD,
	S_BATTLE,
	S_ATTACK_CMD,
	S_NOT_ENOUGH_SUPPLIES,
	S_NOT_ENOUGH_UNITS_TO_MAKE_ATTACK,
	S_ENTITIES_WILL_BE_CREATED,
	S_LAST_SENDER_MSG = S_ENTITIES_UPDATE,
	S_SAY_HELLO
};

struct clientProcessInfo {
	int id;
	int clientPID;
	int clientProcessPID;
};

struct createEntityMessage {
	long type;
	int entity;
	int timeLeft;
};
struct createEntityMessage createEntityMsg;

struct battleMessage {
	long type;
	int attackerID;
	int defenderID;
	int success;
	int attackerUnits[LAST_ENTITY + 1];
};
struct battleMessage battleMsg;

// shm
struct serverShm {
	int units[LAST_ENTITY + 1];
	int wins;
	int creatorListSize;
};
struct serverShm shm;
